<?php
namespace Common\Mysql;
class SiteMysql extends CommonMysql {

    public function addSite($data) {
        $title = v($data,'title');
        verify($title,'notEmpty','网站名称不能为空');

        $domain = v($data,'domain');
        verify($domain,'notEmpty','网站域名不能为空');
        verify($domain,'isDomain','非法网站域名');

        $record = v($data,'record');
        verify($record,'notEmpty','备案号不能为空');

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        if($this->where("`title`='{$title}' OR `domain` = '{$domain}'")->count()) {
            E('网站已经存在');
        }

        $imageInfo = uploadImage('logo','logo');
        $logo = v($imageInfo,'path');
        verify($logo,'notEmpty','网站logo不能为空');

        $id = $this->addRecord(array(
            'title'     =>  $title,
            'domain'    =>  $domain,
            'logo'      =>  $logo,
            'statistic' =>  v($data,'statistic'),
            'keywords'  =>  v($data,'keywords'),
            'description'=> v($data,'description'),
            'record'    =>  $record,
            'createtime'=>  NOW_TIME,
            'updatetime'=>  NOW_TIME,
            'status'    =>  $status,
        ),false,'网站创建成功');

        D('Site','Mongo')->addSite($id);

        return $id;
    }

    public function updateSite($data) {
        $id = v($data,'id');
        verify($id,'notEmpty','网站ID不能为空');
        verify($id,'isID','非法网站ID');

        $title = v($data,'title');
        verify($title,'notEmpty','网站名称不能为空');

        $domain = v($data,'domain');
        verify($domain,'notEmpty','网站域名不能为空');
        verify($domain,'isDomain','非法网站域名');

        $record = v($data,'record');
        verify($record,'notEmpty','备案号不能为空');

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');


        $siteInfo = $this->find($id);
        verify($record,'notEmpty','网站不存在');

        if($this->where("(`title`='{$title}' OR `domain` = '{$domain}') AND `id` <> {$id}")->count()) {
            E('网站已经存在');
        }

        $update_data = array(
            'id'        =>  $id,
            'title'     =>  $title,
            'domain'    =>  $domain,
            'statistic' =>  v($data,'statistic'),
            'keywords'  =>  v($data,'keywords'),
            'description'=> v($data,'description'),
            'record'    =>  $record,
            'updatetime'=>  NOW_TIME,
            'status'    =>  $status,
        );

        if($_FILES['logo']['tmp_name']) {
            $imageInfo = uploadImage('logo','logo');
            if($imageInfo) {
                $update_data['logo'] = v($imageInfo,'path');
            }
        } 

        $id = $this->updateRecord($update_data,false,'网站修改失败');

        return $id;
    }

    public function changeTemplate($id,$tid) {

        verify($id,'notEmpty','网站ID不能为空');

        verify($tid,'notEmpty','模板ID不能为空');

        $siteInfo = $this->find($id);
        verify($siteInfo,'notEmpty','网站不存在');

        return $this->save(array(
            'id'    =>  $id,
            'tid'   =>  $tid,
        ));
    }
}
?>
